<%--
  Created by IntelliJ IDEA.
  User: wuyukai
  Date: 2021/11/8
  Time: 11:18 上午
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html lang="zh-CN">
<%@include file="/WEB-INF/include-head.jsp"%>
<script type="text/javascript" src="jquery/jquery.pagination.js"></script>
<script type="text/javascript">


    $(function (){
        // 调用后面声明的函数对页码导航条进行初始化操作
        initPagination();

        $("#batchDeletion").click(function () {
            var $dx = $("input[name=dx]:checked");
            if ($dx.length == 0) {
                layer.msg("请选择要删除的用户！");
            }else if (layer.confirm("确定要删除吗？")) {
                var pram = "";
                for (var i = 0 ; i < $dx.length ; i++) {
                    pram+="id="+$($dx[i]).val();
                    if (i < $dx.length - 1) {
                        pram+="&";
                    }
                }

                $.ajax({
                    "url" : "admin/batchDeletion/page.html",
                    "type" : "post",
                    "data" : JSON.stringify(pram),
                    "dataType" : "json",
                    "contentType" : "application/json;charset=UTF-8",
                    "success" : function (data) {
                        if (data.success) {
                            window.location.href="admin-page.jsp"
                        }else {
                            layer.msg ("删除用户失败");
                        }
                    }
                })
            }

        });

        // 将添加用户的功能由原来的普通请求改成ajax请求
        $("#showModalBtn").click(function () {
            $("#addAdminModel").modal("show");
        });

        // 点击新增按钮添加新用户
        $("#addAdminBtn").click(function () {
            var adminLoginAcct = $("#addAdminModel [name=adminLoginAcct]").val();
            var adminName = $("#addAdminModel [name=adminName]").val();
            var adminPswd = $("#addAdminModel [name=adminPswd]").val();
            var email = $("#addAdminModel [name=email]").val();
            $.ajax({
                url: "admin/add/info.json",
                type: "post",
                data: {
                    "adminLoginAcct":adminLoginAcct,
                    "adminName":adminName,
                    "adminPswd":adminPswd,
                    "email":email
                },
                dataType: "json",
                success:function (response) {
                    var result = response.result;
                    if (result == "SUCCESS") {
                        layer.msg("用户添加成功！");
                    }
                    if (result == "FAILED") {
                        layer.msg("用户添加失败！失败原因："+response.message);
                        return;
                    }
                },
                error:function (response) {
                    layer.msg("响应状态码："+response.status+"  报错信息："+response.statusText);
                }
            })
            $("#addAdminModel").modal("hide");
            $("#addAdminModel").val("");
        });
    });

    // 生成页码导航条的函数
    function initPagination(){
        // 获取总记录数
        var totalRecord = ${requestScope.pageInfo.total};
        // 声明一个JSON对象存储Pagination要设置的属性
        var properties = {
            num_edge_entries:3,                                 // 边缘页数
            num_display_entries: 5,                             // 主体页数
            callback: pageSelectCallback,                       // 指定用户点击翻页的按钮时跳转页面的回调函数
            items_per_page:${requestScope.pageInfo.pageSize},   // 每页要显示的数据的数量
            current_page: ${requestScope.pageInfo.pageNum - 1} ,// pagination内部使用pageIndex来管理页码，pageIndex从0开始，pageNum从1开始，所以要减一
            prev_text: "上一页",
            next_text: "下一页"
        }

        // 生成页码导航条
        $("#Pagination").pagination(totalRecord,properties);
    }

    function pageSelectCallback(pageIndex,jQuery) {

        // 根据pageIndex计算得到pageNum
        var pageNum = pageIndex + 1;

        // 跳转页面
        window.location.href = "admin/get/page.html?pageNum="+pageNum+"&keyword=${param.keyword}";

        //由于每一个页码按钮都是超链接，所以在这个函数最后取消超链接的默认行为
        return false;
    }
</script>
<body>

<%@include file="/WEB-INF/include-nav.jsp"%>

<div class="container-fluid">
    <div class="row">
        <%@include file="/WEB-INF/include-sidebar.jsp"%>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                    <form action="admin/get/page.html" method="post" class="form-inline" role="form" style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input name="keyword" class="form-control has-success" type="text" placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button type="submit" class="btn btn-warning"><i class="glyphicon glyphicon-search"></i> 查询</button>
                    </form>
                    <button id="batchDeletion" class="btn btn-danger" style="float:right;margin-left:10px;"><i  class=" glyphicon glyphicon-remove"></i> 删除</button>
                    <button id="showModalBtn" class="btn btn-primary" style="float:right;"><i class="glyphicon glyphicon-plus"></i> 新增</button>
                    <br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                            <tr>
                                <th width="30">#</th>
                                <th width="30"><input type="checkbox"></th>
                                <th>账号</th>
                                <th>名称</th>
                                <th>邮箱地址</th>
                                <th width="100">操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <c:if test="${empty requestScope.pageInfo.list}">
                                <tr>
                                    <td colspan="6" align="center">抱歉！没有查询到你要的数据！</td>
                                </tr>
                            </c:if>
                            <c:if test="${!empty requestScope.pageInfo.list}">
                                <c:forEach items="${requestScope.pageInfo.list}" var="admin" varStatus="mystatus">

                            <tr>
                                <td>${mystatus.count}</td>
                                <td><input type="checkbox" name="dx" value="${admin.id}"></td>
                                <td>${admin.loginAcct }</td>
                                <td>${admin.userName }</td>
                                <td>${admin.email }</td>
                                <td>
                                    <button type="button" id="aaa" class="btn btn-success btn-xs"><i class=" glyphicon glyphicon-check"></i></button>
                                    <a  href="admin/editRedirect/${admin.id}.html" class="btn btn-primary btn-xs"><i class=" glyphicon glyphicon-pencil"></i></a>
                                    <a  href="admin/delete/${admin.id}/${requestScope.pageInfo.pageNum}/${requestScope.keyword}.html" class="btn btn-danger btn-xs"><i class=" glyphicon glyphicon-remove"></i></a>
                                </td>
                            </tr>
                            </c:forEach>
                            </c:if>
                            </tbody>
                            <tfoot>
                            <tr>
                                <td colspan="6" align="center">
                                    <div id="Pagination" class="pagination"><!-- 这里显示分页 --></div>
                                </td>
                            </tr>

                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<%@ include file="/WEB-INF/model-admin-add.jsp"%>

</body>
</html>

